package com.hq.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hq.pojo.Account;
import com.hq.pojo.UserInfo;
import com.hq.service.AccountService;
import com.hq.service.impl.AccountImpl;
import com.hq.utils.JSONObjectUtil;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Register被访问");
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String pushId = request.getParameter("pid");
		PrintWriter print = response.getWriter();
		AccountService service = new AccountImpl();
		int a = service.register(creatAccount(name, password, pushId));
		boolean res = a == 0 ? false : true;
		print.print(JSONObjectUtil.createRegisterResult(res,a));
		print.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private Account creatAccount(String name, String password, String pushId) {
		Account account = new Account();
		account.setAccount_name(name);
		account.setAccount_password(password);
		account.setPush_id(pushId);
		account.setCreateAt(null);
		account.setUpdateAt(null);
		return account;
	}
	

}
